package algorithm.stack;

import org.omg.PortableInterceptor.INACTIVE;

import java.util.Stack;

/**
 * 使用递归的方式将栈逆序，不能使用额外的栈
 * Created by zhy on 2017/9/2.
 */
public class Main3 {
    //将栈底元素删除并返回

    public static int getAndRemoveLastElement(Stack<Integer> stack){
        int  reuslt = stack.pop();
        if(stack.isEmpty()){
            return reuslt;
        }else{
            int last = getAndRemoveLastElement(stack);
            stack.push(reuslt);
            return last;
        }
    }
    //逆序栈
    public static void reverse(Stack<Integer> stack){
        if(stack.isEmpty()){
            return;
        }
        int i = getAndRemoveLastElement(stack);
        reverse(stack);
        stack.push(i);
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<Integer>();
        stack.add(1);
        stack.add(2);
        stack.add(3);
        stack.add(4);
        reverse(stack);
        System.out.println(stack.toString());
    }
}
